const _ = require('lodash');

module.exports = async function(self, cmd, ...args){
	switch(cmd){
		case 'add':
			if (args.length>0) {
				if (_.find(self.namespace.targets, {id: args[0]}))
					console.error('already exists.');
				else
					self.namespace.targets.push({id: args[0]});
			}
			break;
		case 'del':
			if (args.length>0) {
				_.remove(self.namespace.targets, item => item.id === args[0]);
			}
			break;
		case 'clear':
			self.namespace.targets = [];
			break;
		case 'toggle':
			if (args.length>0) {
				let target = _.find(self.namespace.targets, {id: args[0]});
				if (target) {
					target.state = (typeof target.state === 'undefined' || target.state)?false:true;
				} else {
					console.error('target is not exists');
				}
			}
			break;
		case 'run':
			if (args.length<1) {
				console.log('usage: ip run 192.168.1.2');
				return;
			}
			// 根据webshell清单批量运行指定target
			let target = _.find(self.namespace.targets, {id: args[0]});
			if (target) {
				for(let ws of self.namespace.webshells) {
					await self.namespace.runHandler(target, ws);
				}
			} else console.error('target not found.');
			break;
		default:
			console.log(self.namespace.targets);
			break;
	}
}